import request from '@/utils/request'

/**
 * 考试管理API服务
 */

/**
 * 获取考试分页列表
 * @param {Object} params 查询参数
 * @param {number} params.page 页码
 * @param {number} params.size 每页条数
 * @param {string} params.keyword 搜索关键词
 * @param {string} params.category 考试分类
 * @param {string} params.status 考试状态
 * @returns {Promise} 返回考试分页数据
 */
export const getExamPage = (params) => {
  return request({
    url: '/api/edu/exam/getExamPage',
    method: 'post',
    data: params
  })
}

/**
 * 获取所有考试列表
 * @returns {Promise} 返回考试列表
 */
export const getExamList = () => {
  return request({
    url: '/api/edu/exam/getExamList',
    method: 'post'
  })
}

/**
 * 根据ID获取考试详情
 * @param {string} id 考试ID
 * @returns {Promise} 返回考试详情
 */
export const getExamById = (id) => {
  return request({
    url: '/api/edu/exam/getExamById',
    method: 'post',
    params: { id }
  })
}

/**
 * 添加或更新考试
 * @param {Object} examData 考试数据
 * @returns {Promise} 返回操作结果
 */
export const saveOrUpdateExam = (examData) => {
  return request({
    url: '/api/edu/exam/addOrUpdateExam',
    method: 'post',
    data: examData
  })
}

/**
 * 批量删除考试
 * @param {Array<string>} ids 考试ID数组
 * @returns {Promise} 返回操作结果
 */
export const deleteExams = (ids) => {
  return request({
    url: '/api/edu/exam/deleteExams',
    method: 'post',
    data: ids
  })
}

/**
 * 更新考试状态
 * @param {string} id 考试ID
 * @param {number} status 考试状态 (0-禁用, 1-启用)
 * @returns {Promise} 返回操作结果
 */
export const updateExamStatus = (id, status) => {
  return request({
    url: '/api/edu/exam/updateStatus',
    method: 'post',
    data: { id, status }
  })
}

/**
 * 获取考试题目列表
 * @param {string} examId 考试ID
 * @returns {Promise} 返回题目列表
 */
export const getExamQuestions = (examId) => {
  return request({
    url: '/api/edu/question/getQuestionList',
    method: 'post',
    params: { examId }
  })
}

/**
 * 添加或更新考试题目
 * @param {Object} questionData 题目数据
 * @returns {Promise} 返回操作结果
 */
export const saveOrUpdateQuestion = (questionData) => {
  return request({
    url: '/api/edu/question/addOrUpdateQuestion',
    method: 'post',
    data: questionData
  })
}

/**
 * 删除考试题目
 * @param {string} id 题目ID
 * @returns {Promise} 返回操作结果
 */
export const deleteQuestion = (id) => {
  return request({
    url: '/api/edu/question/deleteQuestion',
    method: 'post',
    params: { id }
  })
}

/**
 * 获取考试成绩列表
 * @param {Object} params 查询参数
 * @param {number} params.page 页码
 * @param {number} params.size 每页条数
 * @param {string} params.examId 考试ID
 * @returns {Promise} 返回成绩分页数据
 */
export const getExamScores = (params) => {
  return request({
    url: '/api/edu/score/getScorePage',
    method: 'post',
    data: params
  })
}

/**
 * 获取考试统计信息
 * @param {string} examId 考试ID
 * @returns {Promise} 返回考试统计信息
 */
export const getExamStatistics = (examId) => {
  return request({
    url: '/api/edu/exam/getExamStatistics',
    method: 'post',
    params: { examId }
  })
}

/**
 * 导出考试成绩
 * @param {string} examId 考试ID
 * @returns {Promise} 返回导出的文件流
 */
export const exportExamScores = (examId) => {
  return request({
    url: '/api/edu/score/exportScores',
    method: 'post',
    params: { examId },
    responseType: 'blob'
  })
}

export default {
  getExamPage,
  getExamList,
  getExamById,
  saveOrUpdateExam,
  deleteExams,
  updateExamStatus,
  getExamQuestions,
  saveOrUpdateQuestion,
  deleteQuestion,
  getExamScores,
  getExamStatistics,
  exportExamScores
}